package org.example;

import java.sql.*;

public class JdbcConnectCode2 {
    /*
    在静态代码块中添加添加连接
     */
    static Connection con;
    static String url="jdbc:mysql://121.41.77.32:3306/school_demo?useSSL=False";
    static String user = "root";
    static String password="123456";
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        try {
            con = DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws SQLException {
        Statement statement = con.createStatement();
        String sql2 = "select * from school_info";
        ResultSet resultSet = statement.executeQuery(sql2);
        while (resultSet.next()){
            String schoolName = resultSet.getString("school_name");
            String schoolCity = resultSet.getString("school_city");
            System.out.println(String.format("%s\t%s",schoolName,schoolCity));
        }

    }
}

